package com.wxy.ej.flow.modules.act.listen;

import com.rt.easyjava.common.utils.others.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.ExecutionListener;
import org.activiti.engine.delegate.Expression;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;


/**
 * 每个人完成任务，都会执行一下。
 */
@Slf4j
@Component("taskCompleteListener")
@Scope("prototype")
public class TaskCompleteListener implements ExecutionListener {


    private Expression varPre;
    private Expression clearVars;

    @Override
    public void notify(DelegateExecution delegateExecution) {
        log.info("---->act listener: TaskCompleteListener");

        System.out.println("=======流程完成 userTaskCompleteListener ====== NodeName: "+delegateExecution.getCurrentFlowElement().getName());
        System.out.println("EventName:" + delegateExecution.getEventName());
        System.out.println("ProcessDefinitionId:" + delegateExecution.getProcessDefinitionId());
        System.out.println("ProcessInstanceId:" + delegateExecution.getProcessInstanceId());

        // 并发网关时，变量冲突，指定下变量前缀
        String varPreStr = "";
        if (!StringUtils.isNull(varPre)){
            varPreStr = varPre.getExpressionText();
        }


        // 用户任务多实例时，主流程和多实例流程，都执行这个方法
        System.out.println("==========>11111111111 over:"+delegateExecution.getCurrentFlowElement().getName());
        System.out.println("getId:"+delegateExecution.getId());
        System.out.println("is main:"+delegateExecution.getParentId().equals(delegateExecution.getProcessInstanceId()));
        System.out.println("getParentId:"+delegateExecution.getParentId());
        System.out.println("getRootProcessInstanceId:"+delegateExecution.getRootProcessInstanceId());
        System.out.println("getProcessInstanceId:"+delegateExecution.getProcessInstanceId());
        if (!delegateExecution.getParentId().equals(delegateExecution.getProcessInstanceId())) {
            return;
        }
/*

        // 如果用户数==0，return
        Integer assigneeCount = (Integer)delegateExecution.getVariable(varPreStr+"assigneeCount");
        if (assigneeCount==null || assigneeCount.intValue()==0) {
            return;
        }
        */
/*List userListCheck = (List)delegateExecution.getVariable(varPreStr+"userList", false);
        System.out.println("userListCheck:"+userListCheck);
        if (userListCheck!=null) {
            return;
        }*//*


        // user: {answerId=295, userName=test-13222333322, userId=33}
        // 是这个用户完成任务
        List<Map> companyList = (List<Map>)delegateExecution.getVariable(varPreStr+"companyList");
        List<Map> userList = (List<Map>)delegateExecution.getVariable(varPreStr+"userList");
        Map user = (Map)delegateExecution.getVariable(varPreStr+"user");

        System.out.println("user: "+user);
        System.out.println("userList: "+userList);
        System.out.println("companyList: "+companyList);

        // 用户任务时（无子流程），是取规则下所有部门的人，共同执行一个任务，所以，任务完成时，所有部门任务结束
        cityTaskAutoService.completeBizTaskSon(companyList);

        // 流程执行日志
        FlowElement flowElement = delegateExecution.getCurrentFlowElement();
        // ${companyId}
        String doc = ExpressionUtils.parse(flowElement.getDocumentation(), delegateExecution);


        CtcTaskLogBizDto taskLogDto = new CtcTaskLogBizDto();
        taskLogDto.setCtcTaskId(Long.parseLong(delegateExecution.getProcessInstanceBusinessKey()));
        taskLogDto.setFlowElementId(flowElement.getId());
        taskLogDto.setFlowElementName(flowElement.getName());
        taskLogDto.setFlowElementDoc(doc);
        taskLogDto.setCreatedate(DateTool.currentDate("yyyy-MM-dd HH:mm:ss.SSS"));
        StringBuilder sb = new StringBuilder();
        sb.append("流程完成；修改业务表状态：用户任务时（无子流程），是取规则下所有部门的人，共同执行一个任务，所以，任务完成时，所有部门任务结束，所有指派人任务结束");
        taskLogDto.setDescription(sb.toString());
        ctcTaskLogBizService.create(taskLogDto);


        // 是否清除变量
        String clearVarsStr = "false";
        if (!StringUtils.isNull(clearVars)){
            clearVarsStr = clearVars.getExpressionText();
        }
        System.out.println("==========>11111111111 clearVarsStr :"+clearVarsStr);
        if (clearVarsStr.equals("true")) {
            // 人数不删除，给后面网关判断是否有人，是否循环用
            // delegateExecution.removeVariable(varPreStr+"assigneeCount");
            delegateExecution.removeVariable(varPreStr+"userList");
            delegateExecution.removeVariable(varPreStr+"userNoticeList");
            delegateExecution.removeVariable(varPreStr+"companyList");
            delegateExecution.removeVariable(varPreStr+"companyIdList");
            delegateExecution.removeVariable(varPreStr+"companyNameList");

            // condition
            delegateExecution.removeVariable(varPreStr+"cmpyConditionRadio");
            delegateExecution.removeVariable(varPreStr+"cmpyConditionCount");
            delegateExecution.removeVariable(varPreStr+"cmpyConditionRate");
            delegateExecution.removeVariable(varPreStr+"userConditionRadio");
            delegateExecution.removeVariable(varPreStr+"userConditionCount");
            delegateExecution.removeVariable(varPreStr+"userConditionRate");

            CtcTaskLogBizDto taskLogDto1 = new CtcTaskLogBizDto();
            taskLogDto1.setCtcTaskId(Long.parseLong(delegateExecution.getProcessInstanceBusinessKey()));
            taskLogDto1.setFlowElementId(flowElement.getId());
            taskLogDto1.setFlowElementName(flowElement.getName());
            taskLogDto1.setFlowElementDoc(doc);
            taskLogDto1.setCreatedate(DateTool.currentDate("yyyy-MM-dd HH:mm:ss.SSS"));
            StringBuilder sb1 = new StringBuilder();
            sb1.append("清除流程节点变量； userList、companyList、companyIdList；companyNameList");
            taskLogDto1.setDescription(sb1.toString());
            ctcTaskLogBizService.create(taskLogDto1);
        }
*/

    }
}
